package com.qf.conifg;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {


    // 业务交换机
    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange("order-exchange", true, false);
    }

    // 业务队列
    @Bean
    public Queue orderQueue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 5000); // 超时是5s
        map.put("x-dead-letter-exchange", "dle-exchange"); // 死信交换机
        map.put("x-dead-letter-routing-key", "error.order"); // 死信的路由键
        return new Queue("order-queue", true, false, false, map);
    }

    // 把业务队列和业务交换机绑定
    @Bean
    public Binding orderQueueTooRderExchange() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with("order.*");
    }

    // 创建死信交换机
    @Bean
    public TopicExchange dleExchange(){
        return new TopicExchange("dle-exchange",true,false);
    }

    // 死信队列
    @Bean
    public Queue dleQueue(){
        return new Queue("dle-queue",true,false,false);
    }

    @Bean
    public Binding dleQueueTodleExchange() {
        return BindingBuilder.bind(dleQueue()).to(dleExchange()).with("error.*");
    }

}
